import { ChangeDetectionStrategy, Component, OnInit, ViewChild } from '@angular/core';
import { Title } from '@angular/platform-browser';
import * as moment from 'moment';
import { SideMemberPanelComponent } from './embed/side-member-panel/side-member-panel.component';
import { BtnNaviSharedService } from './services/btn-navi-shared.service';
import { Subscription } from 'rxjs/internal/Subscription';
import { AsidePanelService } from './services/aside-panel.service';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
import { FootmarkTrackService } from './services/footmark-track.service';
declare var $: any;
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styles: [``],
  changeDetection: ChangeDetectionStrategy.Default
})
export class AppComponent implements OnInit {
  //是否显示底部全局的导航:首页|版块|消息|我
  public btnNavi: boolean = true;
  @ViewChild('rightSide', { static: false })
  public rightSideSction!: SideMemberPanelComponent;
  public rightSideSubscription!: Subscription;

  constructor(
    private router: Router,
    private title: Title, 
    private asidePanelService: AsidePanelService,
    private footMark: FootmarkTrackService,
    private btnNaviSharedService: BtnNaviSharedService) {
    this.btnNavi = true;
    //一, 路由切换时关闭侧边栏
    //二, 见SideMemberPanelComponent
    this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: any) => {
      this.rightSideSction.closePanel(null);
      //console.log('[App]prev url:', event.url);
      this.footMark.save(event.url);
    });
  }

  ngOnInit(): void {
    this.title.setTitle('Orion jForum');
    this.btnNaviSharedService.getEmittedValue().subscribe((item: boolean) => {
      if (item !== this.btnNavi) {
        this.btnNavi = item; //不一样时赋值,免得触发多余的:ExpressionChangedAfterItHasBeenChecked
      }
    });
    $(document).ready(function () {
      $('body').bootstrapMaterialDesign();
    });
    //://github.com/urish/ngx-moment/issues/108#issuecomment-270166689
    //://momentjs.com/docs/#/i18n/
    //://github.com/ihym/ngx-timeago
    moment.updateLocale('zh', {
      relativeTime: {
        future: "in %s",
        past: "%s",
        s: "刚刚",
        m: "1分钟",
        mm: "%d分钟前",
        h: "1小时",
        hh: "%d小时前",
        d: "昨天",
        dd: "%d天前",
        M: "本月",
        MM: "%d月前",
        y: "本年",
        yy: "%d年前"
      }
    });
    this.rightSideSubscription = this.asidePanelService.query().subscribe(() => this.openSideSection());
  }

  public openSideSection(): void {
    this.rightSideSction.open();
    //console.log('[App]component: ?? right side opened');
  }
}
